package com.meida.boot.handler;

import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayMessage;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.enums.OrderStatusEnum;
import com.meida.common.base.handler.PayInfoHandler;
import com.meida.common.utils.ApiAssert;
import com.meida.module.order.client.entity.OrderInfo;
import com.meida.module.order.provider.service.OrderInfoService;
import com.meida.module.pay.client.entity.PayInfo;
import com.meida.module.pay.provider.handler.PaySuccessHandler;
import com.meida.module.pay.provider.service.PayInfoService;
import com.meida.module.product.client.entity.ProdProduct;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import com.meida.module.vip.provider.service.VipTypeService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 商品支付处理器
 *
 * @author zyf
 */
@Component("orderPayHandler")
public class OrderPayHandler implements PayInfoHandler, PaySuccessHandler {

    @Resource
    private PayInfoService payInfoService;

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private AppUserService userService;


    @Resource
    private VipTypeService vipTypeService;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap requestMap) {
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        AppUser user = userService.getById(userId);
        String userType = user.getUserType();
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave cs, EntityMap params) {
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, Object object) {

    }

    /**
     * 创建支付信息
     *
     * @param cs         对象模型
     * @param requestMap 参数接收
     * @param outTradeNo 订单号
     * @return
     */
    @Override
    public Object getPayInfo(CriteriaSave cs, EntityMap requestMap, String outTradeNo) {
        PayInfo payInfo = cs.getEntity(PayInfo.class);
        OrderInfo orderInfo = orderInfoService.findByOrderNo(outTradeNo);
        ApiAssert.isNotEmpty("订单不存在", orderInfo);
        payInfo.setOrderTitle("商城订单");
        payInfo.setOrderEntity(OrderInfo.class.getSimpleName());
        payInfo.setOrderBody("商城订单");
        payInfo.setBusId(orderInfo.getOrderId());
        payInfo.setOrderEntity(ProdProduct.class.getSimpleName());
        payInfo.setOrderAmount(orderInfo.getPayAmount());
        return payInfo;
    }

    /**
     * 订单回调检验
     *
     * @param payMessage 支付回调消息
     * @param context    上下文参数,可传递到success方法内
     * @param payService 支付服务
     * @param outTradeNo 订单号
     * @return
     */
    @Override
    public boolean validate(PayMessage payMessage, Map<String, Object> context, PayService payService, String outTradeNo) {
        return payInfoService.validate(outTradeNo);
    }


    /**
     * 订单回调业务处理
     *
     * @param payMessage 支付回调消息
     * @param outTradeNo 订单号
     * @param context    上下文参数
     * @param payService 支付服务
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean success(PayMessage payMessage, String outTradeNo, Map<String, Object> context, PayService payService) {
        System.out.println("支付回调" + outTradeNo);
        //获取创建订单时参数
        orderInfoService.paySuccess(outTradeNo, context, OrderStatusEnum.UNSEND.getCode());
        return payInfoService.success(context, OrderStatusEnum.UNSEND.getCode());
    }

}
